﻿
package com.android.finaltracer.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.android.finaltracer.R;
import com.android.finaltracer.engine.EngineService;

public class ToShowMapActivity extends Activity {
    public static final String LOST_PHONE_NUM = "lost";
    private TextView tip_tv;
    private Button ok_btn, cancel_btn, vertify_btn;
    Messenger mService = null;
    private boolean mBound = false;
    private String lost;

    @Override
    protected void onNewIntent(Intent intent) {
        // TODO Auto-generated method stub
        // super.onNewIntent(intent);
        setIntent(intent);
    }

    private ServiceConnection mConn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mService = new Messenger(service);
            mBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            // TODO Auto-generated method stub
            mService = null;
            mBound = false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.toshowmap);
        initIntent();
        findViews();
        initData();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        bindService();
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        // unBindService();
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        unBindService();
    }

    private void initIntent() {
        Intent intent = getIntent();
        lost = intent.getStringExtra(LOST_PHONE_NUM);
    }

    private void findViews() {
        tip_tv = (TextView) findViewById(R.id.tip_tv);
        ok_btn = (Button) findViewById(R.id.ok_btn);
        cancel_btn = (Button) findViewById(R.id.cancel_btn);
        vertify_btn = (Button) findViewById(R.id.vertify_btn);
        tip_tv.setText("您的手机已经丢失，被换成了这个手机号:" + lost + ",是否开始跟踪?");
    }

    private void initData() {
        ok_btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if (!mBound) {
                    return;
                }
                try {
                    mService.send(Message.obtain(null,
                            EngineService.MSG_UI_SERVICE_STATR_CONTROL, lost));
                    Intent intent = new Intent(ToShowMapActivity.this,
                            FuncShowTabMainActivity.class);
                    startActivity(intent);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                finish();
            }
        });
        cancel_btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (!mBound) {
                    return;
                }
                try {
                    mService.send(Message
                            .obtain(null,
                                    EngineService.MSG_UI_SERVICE_NEGATIVE_CONTROL,
                                    lost));
                    Intent intent = new Intent(ToShowMapActivity.this,
                            FuncShowTabMainActivity.class);
                    startActivity(intent);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                finish();
            }
        });
        vertify_btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new AlertDialog.Builder(ToShowMapActivity.this)
                        .setTitle("警告")
                        .setMessage("确认对方手机是合法的换卡吗？确认之后将不会再收到手机丢失的提示")
                        .setPositiveButton("确认",
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface arg0,
                                            int arg1) {
                                        // TODO Auto-generated method stub
                                        if (!mBound) {
                                            return;
                                        }
                                        try {
                                            mService.send(Message
                                                    .obtain(null,
                                                            EngineService.MSG_UI_SERVICE_VERIFY_SUCCESS,
                                                            lost));
                                        } catch (RemoteException e) {
                                            e.printStackTrace();
                                        }
                                        arg0.dismiss();
                                        finish();
                                    }
                                })
                        .setNegativeButton("取消",
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface arg0,
                                            int arg1) {
                                        // TODO Auto-generated method stub
                                        arg0.dismiss();
                                    }
                                });

            }
        });
    }

    private void bindService() {
        if (mBound) {
            return;
        }
        // bind service
        Intent serviceIntent = new Intent(this, EngineService.class);
        bindService(serviceIntent, mConn, Context.BIND_AUTO_CREATE);
    }

    private void unBindService() {
        if (!mBound) {
            return;
        }
        unbindService(mConn);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
